package pers.lan.linkedlist;

import java.util.Arrays;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/9/10 8:07 AM
 */
public class LC_725 {

    public static void main(String[] args) {
        System.out.println(
                Arrays.toString(
                        new LC_725().splitListToParts(
                                ListNode.build(new int[]{1, 2, 3, 4}),
                                5
                        )
                )
        );
    }

    public ListNode[] splitListToParts(ListNode root, int k) {
        int n = 0;
        ListNode cur = root;
        while (cur != null) {
            n++;
            cur = cur.next;
        }
        int mod = n % k;
        int size = n / k;
        ListNode[] res = new ListNode[k];
        cur = root;
        for (int i = 0; i < k && cur != null; i++) {
            res[i] = cur;
            int cursize = size + (mod-- > 0 ? 1 : 0);
            for (int j = 0; j < cursize - 1; j++) {
                cur = cur.next;
            }
            ListNode next = cur.next;
            cur.next = null;
            cur = next;
        }
        return res;
    }
}
